using System;
using System.Collections;
using System.Globalization;
using System.IO;

namespace Seaskyer.WebApp.Utility.BigUpload
{
	//[StrongNameIdentityPermission(SecurityAction.LinkDemand, Name="Sunrise.Web.Upload", PublicKey="0024000004800000940000000602000000240000525341310004000001000100CB860429768BA0BBAC7170DDCCFB0D249FA6FF3906EBE2CCED8E7D30122A3626D6FF80B93943A447153F35F676F3907E2DF715ACA8DF4FB707042C00739360174F4126F8C84D5EF43AAF877CF1F5B5FC8AE349542746771441B23B317464CC233E5DCA86FCB25D84FD63AE1651C9F013497F3B457EED6FF0052E2C14C13DD1A2")]
	/// <summary>
	/// 
	/// </summary>
	public class Resx
	{	
		// Fields
		private static Hashtable asmfiles;
		internal static Hashtable Files;

		// Methods
		static Resx()
		{
			Resx.Files = new Hashtable();
		}
  
		/// <summary>
		/// 
		/// </summary>
		public Resx()
		{
		}
 
		/// <summary>
		/// 
		/// </summary>
		/// <param name="fileName"></param>
		/// <returns></returns>
		public static byte[] LoadAssemblyFiles(string fileName)
		{
			if (fileName == null)
			{
				throw new ArgumentNullException("filename");
			}
			if (!fileName.StartsWith("/"))
			{
				throw new ArgumentException("must starts with '/'", "filename");
			}

			fileName = ("/" + fileName.Remove(0, 1).Replace('/', '.'));

			if (Resx.asmfiles == null)
			{
				Hashtable ht = new Hashtable();
				string[] resNames = typeof(Resx).Assembly.GetManifestResourceNames();

				foreach (string resName in resNames)
				{
					if (resName.StartsWith("Seaskyer.WebApp.Utility.BigUpload.Resources."))
					{
						byte[] fileContent;
						string key = ("/" + resName.Remove(0, "Seaskyer.WebApp.Utility.BigUpload.Resources.".Length));
						key = key.ToLower();

						using (Stream stream = typeof(Resx).Assembly.GetManifestResourceStream(resName))
						{
							fileContent = new byte[stream.Length];
							stream.Read(fileContent, 0, fileContent.Length);
						}

						ht.Add(key, fileContent);
					}
				}

				Resx.asmfiles = ht;
			}

			return ((byte[]) Resx.asmfiles[fileName.ToLower()]);
		}
 
		/// <summary>
		/// 
		/// </summary>
		/// <param name="filename"></param>
		/// <returns></returns>
		public static byte[] LoadFile(string filename)
		{
			byte[] fileContent;
			if (filename == null)
			{
				throw new ArgumentNullException("filename");
			}

			//Define the structure of resource file name.
			object[] objArray = new object[5];
			objArray[0] = CultureInfo.CurrentUICulture;
			objArray[1] = ":";
			objArray[2] = CultureInfo.CurrentCulture.ToString();
			objArray[3] = ":";
			objArray[4] = filename;

			string fullFileName = string.Concat(objArray).ToLower();

			if (Resx.Files.Contains(fullFileName))
			{
				fileContent = ((byte[]) Resx.Files[fullFileName]);
			}
			else
			{
				fileContent = Resx.LoadAssemblyFiles(filename);
				Resx.Files[fullFileName] = fileContent;
			}

			return fileContent;
		}
 
	}
}
